<template>
    <transition>
        <el-dialog :title="dialogType === 'add' ? '添加课程信息' : '修改课程信息'" v-model="showFlag" width="600px" append-to-body
            @close="handleClose" @open="handleOpen">
            <el-form hide-required-asterisk ref="formRef" :rules="formRule" :model="formInfo" label-width="auto"
                style="padding:30px 30px 0 30px">
                <el-form-item label="教室名称" prop="name">
                    <el-input placeholder="教室名称" type="text" v-model="formInfo.name"></el-input>
                </el-form-item>
                <el-form-item label="教室种类" prop="category">
                    <el-select placeholder="教室种类" v-model="formInfo.category">
                        <el-option v-for="e in category" :key="e" :value="e.value" :label="e.label"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="教室容量" prop="capacity">
                    <el-input placeholder="教室容量" type="number" v-model="formInfo.capacity"></el-input>
                </el-form-item>
                <el-form-item style="margin-top: 40px; margin-left: 372px; ">
                    <el-button @click="handleClose" size="mini">取消</el-button>
                    <el-button @click="formSubmit" size="mini" type="primary">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </transition>
</template>

<script setup>
import api from '@/api/api';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';

const props = defineProps({
    dialogType: {
        type: String,
        default: 'add'
    }, showDialog: {
        type: Boolean
    },
    applyId: {
        default: ''
    },
    editForm: {
        type: Object
    }
})
const emit = defineEmits(['dialogClosed'])
const showFlag = ref(false)

const category = [
    { label: '普通教室', value: '0' },
    { label: '实验室', value: '1' },
    { label: '多媒体教室', value: '2' },
    { label: '其他', value: '3' },
]

const formRef = ref(null)

const validateCapacity = (rule, value, callback) => {
    let num = parseInt(value)
    if (0 > num || num > 5000) {
        callback(new Error('请输入0-5000数字'))
    }
    callback()
}
const formRule = ref({
    name: [
        { required: true, message: '请填写教室名称', trigger: 'blur' }
    ],
    category: [
        { required: true, message: '请填写教室种类', trigger: 'blur' }
    ],
    capacity: [
        { required: true, message: '请填写教室容量', trigger: 'blur' },
        { validator: validateCapacity, trigger: 'blur' },
    ],
})
const formInfo = reactive(
    {
        'name': '', 'category': '', 'capacity': ''
    },
)
const formSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            if (props.dialogType == 'add') {
                api.classroom.add(
                    formInfo.name, formInfo.category, formInfo.capacity
                ).then((res) => {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            } else {
                api.classroom.edit(
                    props.applyId, formInfo.name, formInfo.category, formInfo.capacity
                ).then((res) => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            }

        }
    })
}
const handleClose = () => {
    showFlag.value = false
    formRef.value.resetFields()
    emit('dialogClosed')
}
const handleOpen = () => {
    if (props.dialogType==='edit'){
        formInfo.name=props.editForm.name
        formInfo.category=props.editForm.category
        formInfo.capacity=props.editForm.capacity
    }
}

watch(() => props.showDialog, (newValue, oldValue) => {
    showFlag.value = props.showDialog
})

</script>